package com.rs.adminweb.commons.web;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.jaxplat.exceptions.GeneralException;
import com.jaxplat.fileupload.FileAdjust;
import com.jaxplat.fileupload.FileDownload;
import com.jaxplat.fileupload.FileRemove;

public class UploadImgServlet extends UploadServlet
{
    protected final Log logger = LogFactory.getLog(getClass());

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        process(request, response);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        process(request, response);
    }

    public void process(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        request.setCharacterEncoding("UTF-8");
        String method = request.getParameter("method");

        if (this.logger.isDebugEnabled())
        {
            this.logger.debug("current process method is  == " + method);
        }
        try
        {
            if ("upload".equals(method))
            {
                doUpload(request, response);
            }
            else if ("download".equals(method))
            {
                download(request, response);
            }
            else if ("remove".equals(method))
            {
                remove(request, response);
            }
            else if ("adjust".equals(method))
                adjust(request, response);
        }
        catch (Exception e)
        {
            throw new ServletException(e);
        }
    }

    public void download(HttpServletRequest request, HttpServletResponse response) throws GeneralException
    {
        FileDownload fileDownload = new FileDownload();
        fileDownload.download(request, response);
    }

    public void remove(HttpServletRequest request, HttpServletResponse response) throws GeneralException
    {
        FileRemove fileRemove = new FileRemove();
        fileRemove.remove(request, response);
    }

    public void doUpload(HttpServletRequest request, HttpServletResponse response) throws GeneralException
    {
        String contentType = request.getContentType();
        if (contentType.indexOf("multipart/form-data") >= 0)
        {
            FileUpload fileUpload = new FileUpload();
            fileUpload.upload(request, response);
        }
    }

    public void adjust(HttpServletRequest request, HttpServletResponse response) throws GeneralException
    {
        FileAdjust fileAdjust = new FileAdjust();
        fileAdjust.adjust(request, response);
    }

    public void init() throws ServletException
    {
    }

    public void destroy()
    {
        super.destroy();
    }
}
